package com.vcc.service.client.vo.user;

import com.vcc.core.util.DateUtil;
import com.vcc.core.util.RegexUtil;
import com.vcc.core.util.StringUtil;
import com.vcc.data.entity.Role;
import com.vcc.data.entity.User;
import com.vcc.data.entity.Wallet;
import com.vcc.service.client.vo.baseVo.BaseVo;
import com.vcc.service.client.vo.wellat.WalletVo;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

@EqualsAndHashCode(callSuper = true)
@Data
@Setter
@Getter
public class UserVo extends BaseVo implements Serializable {

    private String username;

    private String account;

    private String email;

    private String registerDate;

    private String roleCode;

    private String roleName;

    private String remark;

    private Integer status;

    private Long parentId;

    private Boolean hasParent;

    private String dealerAccount;

    private List<WalletVo> walletVos;

    private List<String> permissions;

    private Integer kycType;

    private Boolean hasSetPayPassword;

    private Boolean hasSubmitKyc;


    private Integer kycStatus;

    private String clientWebPhone;

    private String invitationCode;

    private Integer registerCount;

    private Boolean hasShareAccount;

    public UserVo() {
    }

    public UserVo(User user, Role role, List<Wallet> wallets,Long hasShareAccount) {
        super.setId(user.getId());
        this.username = user.getUsername();
        this.account = StringUtil.hideMobile(user.getAccount());
        this.email = StringUtil.hideEmail(user.getEmail());
        this.registerDate = DateUtil.format("yyyy-MM-dd", user.getCreateDate());
        this.roleCode = role.getCode();
        this.roleName = role.getName();
        this.parentId = user.getParentId();
        this.remark = user.getRemark();
        this.status = user.getStatus();
        this.hasParent = user.getBeLongToDealer() == null || user.getBeLongToDealer() == 0;
        if (RegexUtil.checkStringIsNotNull(user.getDealerAccount())) {
            this.dealerAccount = StringUtil.hideMobile(user.getDealerAccount());
        } else {
            this.dealerAccount = "";
        }
        this.walletVos = wallets.stream().map(WalletVo::new).collect(Collectors.toList());
        if (StringUtils.isBlank(user.getExpandOne())) {
            this.kycType = null;
        } else {
            this.kycType = Integer.valueOf(user.getExpandOne());
        }
        this.hasSetPayPassword = StringUtils.isNotBlank(user.getPayPassword());
        this.invitationCode = user.getInvitationCode();
        this.registerCount = user.getRegisterCount();
        this.hasShareAccount = hasShareAccount != null && hasShareAccount > 0;
    }


    public UserVo(User user, Role role, List<Wallet> wallets, List<String> permissions, Integer kycStatus, Integer kycType, String clientWebPhone,Long shareAccountCount) {
        super.setId(user.getId());
        this.username = user.getUsername();
        this.account = user.getAccount();
        this.email = user.getEmail();
        this.registerDate = DateUtil.format("yyyy-MM-dd", user.getCreateDate());
        this.roleCode = role.getCode();
        this.roleName = role.getName();
        this.parentId = user.getParentId();
        this.remark = user.getRemark();
        this.status = user.getStatus();
        this.hasParent = user.getParentId() == null || user.getParentId() == 0;
        if (RegexUtil.checkStringIsNotNull(user.getDealerAccount())) {
            this.dealerAccount = user.getDealerAccount();
        } else {
            this.dealerAccount = "";
        }
        this.walletVos = wallets.stream().map(WalletVo::new).collect(Collectors.toList());
        this.permissions = permissions;
        this.hasSetPayPassword = StringUtils.isNotBlank(user.getPayPassword());
        if (this.hasParent) {
            if (kycStatus != null) {
                this.hasSubmitKyc = true;
                this.kycStatus = kycStatus;
                this.kycType = kycType;
            } else {
                this.hasSubmitKyc = false;
            }
        } else {
            this.hasSubmitKyc = true;
            this.kycStatus = 0;
        }
        this.clientWebPhone = clientWebPhone;
        this.invitationCode = user.getInvitationCode();
        this.registerCount = user.getRegisterCount();
        this.hasShareAccount = shareAccountCount != null && shareAccountCount > 0;
    }
}
